package cn.clq.ymcc.web.controller;

import cn.clq.ymcc.service.ILoginService;
import cn.clq.ymcc.domain.Login;
import cn.clq.ymcc.query.LoginQuery;
import cn.clq.ymcc.result.JSONResult;
import cn.clq.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    public ILoginService loginService;

    //登录成功后重定向地址
    @RequestMapping("/loginSuccess")
    public String loginSuccess(){
        return "登录成功";
    }

    /**
    * 保存
    */
    @PostMapping("/save")
    public JSONResult save(@RequestBody Login login){
        loginService.insert(login);
        return JSONResult.success(login.getId());//返回自增ID
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
        loginService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping(value = "/{id}")
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(loginService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('login:list')")
    public JSONResult list(){
        return JSONResult.success(loginService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JSONResult page(@RequestBody LoginQuery query){
        Page<Login> page = new Page<Login>(query.getPage(),query.getRows());
        page = loginService.selectPage(page);
        return JSONResult.success(new PageList<Login>(page.getTotal(),page.getRecords()));
    }
}
